TODO: better pattern recognition