fix var name
added more functionality and error checks