Fixed some bugs. Made some refactoring.