finished refactoring and reordering the code