Small performance improvement and bug fix