A working version. Still may need some fixes