Cleaned up version of https://forums.alliedmods.net/showthread.php?t=152216