added better validation, fixed some bug