More code and comments added, fixed format