Removed some redundant code and re-organized some code.