Some code cleanup: move global functions into 'app' namespace, added comments, removed unused functions