abstracted some code, a few tweaks