various improvements based on IRC feedback
improved structure
use util function instead of checking for null