Bug fixing and code cleanup in response to pull request remarks