Added error message, improved code