Updated version with type classes