Added input sanitization and performed some general cleanup