Edited Hunter Perschy's code