Major refactoring, made code modular