A working version, no noticable bugs