Minor bug fixing and code clarification