Fixing bugs, small improvements, refactoring