cleanup and refactoring of code