Working version. Still some bugs.