fix negative integral bug
performance enhancement
minor variable refactoring