Corrected bugs and typos, cleaned code