Simplified the code, still not quite right