Got it working, fixed some bugs
bug fixes, refactoring