Added a minor optimization and bug fix