fixed some bugs, refactored a bit
fixed some bugs, refactored a bit