Some less broken code
Merged with Chris's code.