Modified/fixed error with offset.
Cleaned up code.