added more validation and fixed bugs