Initial implementation, slightly messy