ugly but functional version