Editted main code slightly