Added LICENSE. Refactor code and some bugs fixed