Cleaned code, added some comments