Minor bug changes and formatted and commented code