Initial import.
Some fixes to avoid going out of bounds.