Handled errors and improved button function