added bugfix and comments from @joeybaker