Fixed some bugs. Still not the final version