Import of current code

Based on Matthew Wong's work