Various small improvements to code organization and performance

Signed-off-by: thisischris <c@ia.net>