Added basic comments
added logging and some error checking