Improve encode performance

Partial fix for #1, see for improvements.

Yeah so the `continue;` statements look rather weird. I did that for the
comments before the if statements, but I can change that if preferred.