Cleaned code for better performance and simplicity