EDIT: Cleaned up code