fixes bugs, refactors code