Working version. Almost final. Bugs to be fixed though