Minor bug fixes and variable optimization