Still have bugs
Tried to make it more readable