Adding a delay makes it work -_-
Re-arranging code for maintainability