error handling, overflow fix, add comments