Improved code thanks to http://www.reddit.com/r/webdev/comments/1q8t14/rate_my_thing_bootstrap_tree_view_with_checkboxes/cdad7pw