fixed lots of bugs, fixed #4, fixed #10