Perform general code improvement

Re-order code.
Check and add comments.
Remove console.log (these are less needed now).