refactored code and added loader