Adapted code to Jordie0608's fix