Refactoring. Some bugs. I have no idea this time. Resolved #47.