Performance improvements, simplifications and bug fixes.

Signed-off-by: Ross Allan <rallanpcl@gmail.com>
Merge pull request #134 from nallar/master

Performance improvements, simplifications and bug fixes.