Refactor autoloading, fix #4048