[#2 #4] Bugs. Major refactoring.