New version with better types