Partial answer to a question on stackoverflow