rewrote some code and fixed small bug
further refactors