Code cleanup, did some renaming and updated comments to clarify behavior.