Refactored code and removed prepare errors