Simplify the code.

Avoid too much nesting of code.
Open file using the with keyword, avoid calling f.close() explicitly.
Merge pull request #25 from abhinav-upadhyay/simplify

Simplify the code.