Refactoring + commenting, fix bugs