retarded algorithm that should work but doesn't