Cleaned code and added validation for messages