Added loader and some code refactoring