More complete implementation, fixed some rendering bugs.