Added a cleaner version, fewer bugs and more OO