Fixed bugs, added more detailed documentation, refactored