Refactored the code and removed some int casts that where not require