Added error checking and functions