fixing bugs, refactor